package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.CustomerDao;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.entity.Customer;
import com.atguigu.jxc.entity.Goods;
import com.atguigu.jxc.service.CustomerService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
@Service
public class CustomerServiceImpl implements CustomerService {

    @Autowired
    private CustomerDao customerDao;
    /**
     * 客户列表分页
     * @param page
     * @param rows
     * @param customerName
     * @return
     */
    @Override
    public Map<String, Object> list(Integer page, Integer rows, String customerName) {

        PageHelper.startPage(page, rows);
        Page<Customer> pageCustomer = customerDao.selectPage(customerName);
        Map<String, Object> map = new HashMap<>();
        map.put("total",pageCustomer.getTotal());
        map.put("rows",pageCustomer);
        return map;
    }

    /**
     * 添加或者修改
     * @param customer
     * @return
     */
    @Override
    public ServiceVO saveOrUpdate(Customer customer) {
        Integer customerId = customer.getCustomerId();
        Customer selectCustomer = customerDao.selectCustomer(customerId);
        if (null != selectCustomer){
            //修改
            customerDao.update(customer);
        }else {
            //添加
            customerDao.save(customer);
        }
        return new ServiceVO(100,"请求成功");
    }

    /**
     * 删除
     * @param ids
     * @return
     */
    @Override
    public ServiceVO delete(String[] ids) {
        for (String id : ids) {

            customerDao.delete(id);
        }
        return new ServiceVO(100,"请求成功");
    }
}
